<?php

namespace App\Service;
use http\Env;
use Hyperf\DbConnection\Db;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\Annotation\CachePut;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Database\Transaction\Transaction;

class RewardService
{
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redis = $container->get(\Hyperf\Redis\Redis::class);
    }
    public function reward_records_insert($data)
    {
        $order_id = time().mt_rand(10000,99999).
            $data['applet_id']
            .$data['user_id']
            .$data['playlet_id']
            .$data['playlet_number']
            .$data['ad_unit_id'];
        $data['order_id'] = $order_id;
        Db::table("reward_records")->insertGetId($data);
        return $order_id;
    }
    public function get_order($order_id) {
        $order_info = Db::table('reward_records')->where('order_id',$order_id)->where('type',1)->first();
        return $order_info;
    }
    public function chech_sign($sign,$order_id,$time): bool
    {
        $secret = 'b7f4909cddf5ee25';
        $check_sign = md5($secret.$order_id.$time);
        if($check_sign === $sign) {
            return true;
        } else {
            return false;
        }
    }
    public function sendGoods($order_info) {
        #剧集是1,2的形式,切割逗号获取
        $playlet_number = explode(',',$order_info->playlet_number);
        $data = [];
        $inser_data = [];
        $inser_data['user_id'] = $order_info->user_id;
        $inser_data['applet_id'] = $order_info->applet_id;
        $inser_data['playlet_id'] = $order_info->playlet_id;
        $inser_data['create_at'] = date('Y-m-d H:i:s');
        $inser_data['order_id'] = $order_info->order_id;
        $inser_data['amount'] = 0;
        $inser_data['tpye'] = 2;
        #一集还是两集
        if(is_array($playlet_number)){
            $redis_key = "log:playlet:pay:".$order_info->applet_id.":".$order_info->playlet_id;
            $playlet_log_list = $this->redis->hGet($redis_key,$order_info->user_id);
            $playlet_log_list = json_decode($playlet_log_list,true);
            foreach ($playlet_number as $key => $value) {
                if(!empty($playlet_log_list)) {

                    if(in_array($value,$playlet_log_list)) {
                        continue;
                    }
                }
                $inser_data['playlet_number'] = $value;
                $data[] = $inser_data;

            }
        }
        if($data) {
            Db::table('data_user_pay_playlet_log')->insert($data);
            return true;
        } else {
            return false;
        }
    }
    public function update_reward_records($order_info,$end_time,$type=3) {
        Db::table('reward_records')
            ->where('order_id',$order_info->order_id)
            ->update([
                'type' => $type,
                'end_time' => $end_time,
                'update_time' => date('Y-m-d H:i:s',time())
            ]);
    }
}